//
//  ZQBaseTableViewPage.m
//  jianzhik
//
//  Created by huami on 2018/8/28.
//  Copyright © 2018年 WKWebView. All rights reserved.
//

#import "ZQBaseTableViewPage.h"
#import "ZQSaveModels.h"
#import <Realm/Realm.h>
#import "UIBarButtonItem+item.h"
#import "ZQSaveModels.h"
@interface ZQBaseTableViewPage ()
/** model */
@property (nonatomic, strong) ZQSaveModels *saveLoginModel;
@end

@implementation ZQBaseTableViewPage

- (UIButton *)footButton
{
    if (!_footButton) {
        UIButton *footButton = [[UIButton alloc] init];
        footButton.frame = CGRectMake(0, UScreenHeight - 47 - kBottomLineHeight, UScreenWidth, 47);
        [footButton setBackgroundColor: [UIColor stringToColor:@"#6eabf6"]];
        [footButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [footButton setTitle:@"添加管理员" forState:UIControlStateNormal];
        footButton.titleLabel.font = [UIFont systemFontOfSize:19];
        [self.view addSubview:footButton];
        _footButton = footButton;
    }
    return _footButton;
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.frame = CGRectMake(0, 0, UScreenWidth, UScreenHeight - kBottomLineHeight);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 47, 0);
    }
    return _tableView;
}

- (void)registerCell:(Class)cls
{
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass(cls) bundle:nil] forCellReuseIdentifier:kCellID];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [NSString stringWithFormat:@"%zd----%p",indexPath.row,cell];
    NSLog(@"%@",cell.textLabel.text);
    return cell;
}


- (void)saveData:(ZQSaveModels *)zqLoginModels
{
    NSString* doucmentpath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
    NSLog(@"沙盒路径%@",doucmentpath);
    NSLog(@"存数据");
    
    RLMResults<ZQSaveModels *> *temp = [ZQSaveModels objectsWhere:@"Username = %@",zqLoginModels.Username];
    NSLog(@"%@",temp);
    if (temp.count == 0) {
        [[RLMRealm defaultRealm] transactionWithBlock:^{
            [[RLMRealm defaultRealm] addObject:zqLoginModels];
        }];
    } else {
        NSLog(@"数据库已经有该条数据不需要再存了");
    }
}

- (ZQSaveModels *)queryDataWithValue:(NSString *)queryValue
{
    
    NSLog(@"查询");
    self.saveLoginModel = [[ZQSaveModels objectsWhere:@"Username = %@",queryValue]firstObject];
    NSLog(@"取出来的数据%@",self.saveLoginModel);
    //    NSString *querDataStr = [NSString stringWithFormat:@"Username = admiin"];
    //    [[ZQSaveModels objectsWhere:@"Username=%@",admin]firstObject];
    ////    _messreaml=[[ZQSaveModels objectsWhere:querDataStr] firstObject];
    //    NSLog(@"取出来的数据%@",[[ZQSaveModels objectsWhere:querDataStr] firstObject]);
    return  self.saveLoginModel;
}

- (void)changeDataQueryDataWithValue:(id)queryValue  model:(ZQSaveModels *)saveModel

{
    NSLog(@"改数据");
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    if (queryValue) {
        [realm beginWriteTransaction];
        [ZQSaveModels createOrUpdateInRealm:realm withValue:@{@"Username":@"admin",@"Token": @"1"}];
      
        [realm commitWriteTransaction];

    } else {
        [realm transactionWithBlock:^{
            [realm addOrUpdateObject:saveModel];
        }];
    }
}

- (ZQSaveModels *)saveLoginModel
{
    if (!_saveLoginModel) {
        _saveLoginModel = [[ZQSaveModels alloc] init];
    }
    return _saveLoginModel;
}


@end
